jQuery(document).ready(function($) {

   $("#settingbasicform #id_state").jobLocation({
      getcityurl: getcityurl,
      getprovinceurl: getprovinceurl
   });

   $("#settingbasicform .useravatar a").bind("click", function() {
      storageSet("changeavatar_redirect", window.location.href);
      return true;
   });

   // use static rule since metadata will be overwrite by others
   $.metadata.setType("attr", "validate");
   var validator = {
      rules: {
         email: {
           remote: {url: validateemailurl, type: "POST"}
         },
         comp_name: {
           remote: {url: validatenameurl, type: "POST"}
         },
         sub_domain: {
           remote: {url: validatesubdomainurl, type: "POST"}
         },

         id_comp_type: { required: true },
         id_industry: { required: true },
         id_size_type: { required: true },
         id_state: { required: true },
         id_city: { required: true },
         phone: { required: true, phone: true},
         contact_name: { required: true, notalldigits:true},
         address: { required: true }
      },

      messages: {
         id_comp_type: {
            required: "请选择公司性质"
         },
         id_industry: {
            required: "请选择公司行业"
         },
         phone: {
            phone: "请输入正确的联系电话"
         },
         contact_name: {
            notalldigits:"联系人不能为纯数字"
         }
      },
      submitHandler: function(form) {
         $(form).ajaxSubmit({
           dataType: "json",
           beforeSubmit: function(data, form, options) {
               if(!showLoading(form))
                  return false;
               $(".server", form).hide();

               if ($('#sub_domain').length > 0) {
                  if ($('#sub_domain', form).val().length >= 5) {
                     $('#sub_domain', form).attr("disabled","disabled");
                  };
               };
            },
            success: function(data) {
              try {
                 var retmsg = data;
                 if(retmsg.error) { $(".server", form).jobUtil('showError', retmsg.error); }
                 else if(retmsg.success) {
                     if($("#comp_name", form).length > 0) 
                        $("#topmenu ul.user_area a.companyname").html(escapeHTML($("#comp_name", form).val()));

                     $(".server", form).jobUtil('showSuccess', retmsg.success, function(){
                        $(".server", form).fadeOut(3000);
                     });
                 }

              } catch(e) {
                 return false;
              }
            }
        });
      }
   };

   $("#settingbasicform").validate(validator);

   /* summary form */
   $("#settingsummaryform").validate(validator);

   $('#settingsummaryform textarea.autogrow').autogrow({
      lineHeight : 16,
      minHeight  : 100,
      maxHeight  : 200
   });
   $('#settingsummaryform textarea[maxcharlength]').each(function() {
      $(this).jobUtil("setInputLimit");
   });

   /* password form */
   $("#settingpasswordform").validate({
      rules: {
         newpassword_verify: {
            required: true,
            equalTo: "#newpassword"
         }
      },
      messages: {
         newpassword_verify: {
            equalTo: "两个新密码不统一"
         }
      },

      submitHandler: function(form) {
         $(form).ajaxSubmit({
           dataType: "json",
           beforeSubmit: function(data, form, options) {
               if(!showLoading(form))
                  return false;
               $(".server", form).hide();
            },
            success: function(data) {
              try {
                 var retmsg = data;
                 if(retmsg.error) { $(".server", form).jobUtil('showError', retmsg.error); }
                 else if(retmsg.success) {
                     $(".server", form).jobUtil('showSuccess', retmsg.success, function(){
                        $(".server", form).fadeOut(3000);
                     });
                 }
              } catch(e) {
                 return false;
              }
            }
        });
      }
  });

   $("#settingpasswordform #newpassword").blur(function() {
      if($(this).val().length <= 0)
         return;

      if(!$(this).jobUtil("strengthCheck"))
         $(this).nextAll(".label-warning").fadeIn();
      else
         $(this).nextAll(".label-warning").fadeOut();
   });

   $("#settingbasicform .c_privacysetting .dropdown-menu a").click(function(){
      var setting = $(this).closest('.c_privacysetting'); 
      var privacyinput = setting.data('privacyinput'); 
      $('#'+privacyinput).val($(this).data('privacy'));
      setting.find('a.dropdown-toggle b').text($(this).text());
      return true; 
   });

   $('#settingbasicform').on('click','#industry_name,#selectindustry',function(){
      var target =  $(this);
      $.ajax({
        type:'GET',
        url:OC.filePath('company', 'ajax', 'getindustry.php'),
        dataType: 'html',
        data:{id_industry:$("#id_industry").val()},
        beforeSend: function(xhr, settings) {
           showLoading(target, settings, null, {button:true});
        },
        success: function(result) {
           $("#selectindustry_dialog .modal-body").html(result);
           // modal changed from fix to absolute, so the dialog is only on top part of view window -- need to be adjusted to be centered 
           var scrolltop = $(window).scrollTop();
           $("#selectindustry_dialog").css({top:scrolltop+154}).modal('show');
        }
      });
      return false;
   })

});
